/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import hibernate.entity.Account;
import hibernate.entity.ReceivedMessage;
import hibernate.entity.SentMessage;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author Tùng
 */
public class MessageDAO {

    private Session session;

    public MessageDAO() {
        SessionFactory sf = Common.HibernateUtil.getSessionFactory();
        session = sf.getCurrentSession();
    }

    public boolean sendMessage(int accountID, String sentTo, String subject, String content) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Account account = (Account) session.get(Account.class, accountID);
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            String sentDate = dateFormat.format(date);
            SentMessage mess = new SentMessage(account, sentTo, subject, content, sentDate);
            session.save(mess);
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean receivedMessage(int accountID, String receivedFrom, String subject, String content, String receivedStatus) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Account account = (Account) session.get(Account.class, accountID);
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            String receivedDate = dateFormat.format(date);
            ReceivedMessage mess = new ReceivedMessage(account, receivedFrom, subject, content, receivedDate, receivedStatus);
            session.save(mess);
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateReceivedMessage(int ReceivedID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            ReceivedMessage mess = (ReceivedMessage) session.get(ReceivedMessage.class, ReceivedID);
            mess.setReStatus("read");
            session.update(mess);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public ReceivedMessage checkReMess(int reMessID) {
        ReceivedMessage mess = new ReceivedMessage();
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from ReceivedMessage Where ReceivedID like ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, reMessID);
            mess = (ReceivedMessage) query.uniqueResult();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return null;
        }
        return mess;
    }

    public SentMessage checkSeMess(int reMessID) {
        SentMessage mess = new SentMessage();
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from SentMessage Where SentID like ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, reMessID);
            mess = (SentMessage) query.uniqueResult();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return null;
        }
        return mess;
    }

    public List<ReceivedMessage> getReceivedMessage(int account_Id) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from ReceivedMessage where ReAccountID = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, account_Id);
            List<ReceivedMessage> result = query.list();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public List<SentMessage> getSentMessage(int account_Id) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from SentMessage where SeAccountID = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, account_Id);
            List<SentMessage> result = query.list();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public boolean deleteReMess(int receivedID) {
        try {
            session.getTransaction().begin();
            ReceivedMessage rm = (ReceivedMessage) session.get(ReceivedMessage.class, receivedID);
            session.delete(rm);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteSeMess(int sentID) {
        try {
            session.getTransaction().begin();
            SentMessage sm = (SentMessage) session.get(SentMessage.class, sentID);
            session.delete(sm);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteSentMessagebyAccountID(int accountID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "delete SentMessage where SeAccountID = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, accountID);
            query.executeUpdate();
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteReceivedMessagebyAccountID(int accountID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "delete ReceivedMessage where ReAccountID = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, accountID);
            query.executeUpdate();
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }
}
